package allinpay

import (
	"com.cyt.yunpay/app/service/pay/allinpay/allinpay"
	"com.cyt.yunpay/app/service/pay/allinpay/core"
	"com.cyt.yunpay/app/service/pay/allinpay/pem"
	allinpayService "com.cyt.yunpay/app/service/pay/allinpay/service"
	"errors"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gconv"
	"reflect"
)

var (
	Pay      *allinpay.AllInPay
	backUrl  = "https://xxx.com/api/v1/backurl"  //填写一个可以访问的url
	frontUrl = "https://xxx.com/api/v1/fronturl" //填写一个可以访问的url
)

/**
 * @note: 通联支付接口类
 * @author：xyt
 */
func init() {
	var err error
	Pay, err = allinpay.New(&core.Config{
		RequestUrl:  "http://116.228.64.55:6900/service/soa",
		PrivateData: pem.PrivateData,
		PublicData:  pem.PublicData,
		Sysid:       "1902271423530473681",
		IsDebug:     true,
		RspCallback: func(service, method string, req map[string]interface{}, rsp interface{}, isFailed bool, failReason string) interface{} {
			//reqStr, _ := json.Marshal(req)
			//rspStr, _ := json.Marshal(rsp)
			//fmt.Printf("+++RspCallback: %s:%s, %s, %s, %t, %s\n", service, method, reqStr, rspStr, isFailed, failReason)
			return rsp
		},
	})
	if err != nil {
		panic(err)
	}
}

// 会员服务

// 创建会员
func MemberServiceCreateMember(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.CreateMemberReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.CreateMember(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 发送短信验证码
func MemberServiceSendVerificationCode(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.SendVerificationCodeReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.SendVerificationCode(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 绑定手机
func MemberServiceBindPhone(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.BindPhoneReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.BindPhone(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 解绑手机
func MemberServiceUnBindPhone(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.UnbindPhoneReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.UnbindPhone(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 电子协议页面
func MemberServiceSignContractPage(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.SignContractReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.SignContractPage("", req)
	if err != nil {
		return nil, err
	}
	result := g.Map{
		"url": res,
	}
	return result, nil
}

// 会员电子协议签约
func MemberServiceSignContract(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.SignContractReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.SignContract(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 个人实名认证
func MemberServiceSetRealName(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.SetRealNameReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.SetRealName(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 设置企业信息
func MemberServiceSetCompanyInfo(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.SetCompanyInfoReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.SetCompanyInfo(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 影印件采集
func MemberServiceUploadIdcardCollect(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.UploadIdcardCollectReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.UploadIdcardCollect(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 企业信息审核结果通知
func MemberServiceVerifyResult(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.VerifyResultReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.VerifyResult(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 获取会员信息(个人)
func MemberServiceGetMemberInfoForIndividual(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.GetMemberInfoReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.GetMemberInfoForIndividual(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 获取会员信息(企业)
func MemberServiceGetMemberInfoForCompany(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.GetMemberInfoReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.GetMemberInfoForCompany(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 查询卡 bin
func MemberServiceGetBankCardBin(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.GetBankCardBinReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.GetBankCardBin(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 申请绑定银行卡
func MemberServiceApplyBindBankCard(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.ApplyBindBankCardReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.ApplyBindBankCard(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 绑定银行卡
func MemberServiceBindBankCard(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.BindBankCardReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.BindBankCard(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 解绑银行卡
func MemberServiceUnbindBankCard(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.UnbindBankCardReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.UnbindBankCard(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 查询银行卡
func MemberServiceQueryBankCard(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.QueryBankCardReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.QueryBankCard(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 锁定会员
func MemberServiceLockMember(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.LockMemberReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.LockMember(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 解锁会员
func MemberServiceUnlockMember(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.UnlockMemberReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.UnlockMember(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 会员绑定支付账户用户标识
func MemberServiceApplyBindAcct(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.ApplyBindAcctReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.ApplyBindAcct(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 修改绑定手机(银行卡验证)
func MemberServiceBankCardChangeBindPhone(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.BankCardChangeBindPhoneReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.BankCardChangeBindPhone(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 修改绑定手机(银行卡验证)
func MemberServiceVerifyBankCardChangeBindPhone(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.VerifyBankCardChangeBindPhoneReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.VerifyBankCardChangeBindPhone(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 会员子账号开通
func MemberServiceCreateBankSubAcctNo(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.CreateBankSubAcctNoReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.CreateBankSubAcctNo(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 设置支付密码【密码验证版】
func MemberServiceSetPayPwd(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.SetPayPwdReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.SetPayPwd(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 修改支付密码【密码验证版】
func MemberServiceUpdatePayPwd(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.UpdatePayPwdReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.UpdatePayPwd(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 重置支付密码【密码验证版】
func MemberServiceResetPayPwd(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.ResetPayPwdReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.ResetPayPwd(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 修改绑定手机【密码验证版】
func MemberServiceUpdatePhoneByPayPwd(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.UpdatePhoneByPayPwdReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MemberService.UpdatePhoneByPayPwd(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 商家服务

// 商户集合对账文件下载
func MerchantServiceGetCheckAccountFile(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.GetCheckAccountFileReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MerchantService.GetCheckAccountFile(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 查询保留资金余额
func MerchantServiceQueryReserveFundBalance(params map[string]interface{}) (map[string]interface{}, error) {
	res, err := Pay.MerchantService.QueryReserveFundBalance()
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 会员子账号汇款入金上账通知
func MerchantServiceNotifySubAcctReserveFundIn(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.NotifySubAcctReserveFundInReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MerchantService.NotifySubAcctReserveFundIn(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 查询商家余额
func MerchantServiceQueryMerchantBalance(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.QueryMerchantBalanceReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MerchantService.QueryMerchantBalance(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 查询银行卡余额
func MerchantServiceQueryBankBalance(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.QueryBankBalanceReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MerchantService.QueryBankBalance(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 平台银行存管账户余额查询
func MerchantServiceQueryBankInExpDetail(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.QueryBankInExpDetailReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MerchantService.QueryBankInExpDetail(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 电子回单下载
func MerchantServiceEleReceiptDownload(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.EleReceiptDownloadReq{}
	gconv.Struct(params, &req)
	res, err := Pay.MerchantService.EleReceiptDownload(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 订单服务

// 充值
func OrderServiceDepositApply(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.DepositApplyReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.DepositApply(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 提现申请
func OrderServiceWithdrawApply(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.WithdrawApplyReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.WithdrawApply(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 消费申请
func OrderServiceConsumeApply(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.ConsumeApplyReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.ConsumeApply(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 托管代收申请(标准版)
func OrderServiceAgentCollectApply(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.AgentCollectApplyReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.AgentCollectApply(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 单笔托管代付(标准版)
func OrderServiceSignalAgentPay(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.SignalAgentPayReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.SignalAgentPay(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 批量托管代付(标准版)
func OrderServiceBatchAgentPay(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.BatchAgentPayReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.BatchAgentPay(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 确认支付 (分前台和后台)
func OrderServicePay(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.PayReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.Pay(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// GetPayCodePage
func OrderServiceGetPayCodePage(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.PayReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.GetPayCodePage("", req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// GetPayPasswordPage
func OrderServiceGetPayPasswordPage(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.PayReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.GetPayPasswordPage("", req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 商品录入
func OrderServiceEntryGoods(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.EntryGoodsReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.EntryGoods(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 商品查询
func OrderServiceQueryGoods(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.QueryGoodsReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.QueryGoods(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 冻结金额
func OrderServiceFreezeMoney(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.FreezeMoneyReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.FreezeMoney(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 解冻金额
func OrderServiceUnfreezeMoney(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.UnfreezeMoneyReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.UnfreezeMoney(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 退款申请
func OrderServiceRefund(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.RefundReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.Refund(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 平台转账
func OrderServiceApplicationTransfer(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.ApplicationTransferReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.ApplicationTransfer(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 查询余额
func OrderServiceQueryBalance(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.QueryBalanceReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.QueryBalance(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 查询订单状态
func OrderServiceGetOrderDetail(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.GetOrderDetailReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.GetOrderDetail(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 查询账户收支明细
func OrderServiceQueryInExpDetail(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.QueryInExpDetailReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.QueryInExpDetail(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 付款方资金代付明细查询
func OrderServiceGetPaymentInformationDetail(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.GetPaymentInformationDetailReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.GetPaymentInformationDetail(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 收款方在途资金明细查询
func OrderServiceGetPayeeFundsInTransitDetail(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.GetPayeeFundsInTransitDetailReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.GetPayeeFundsInTransitDetail(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 订单分账明细查询
func OrderServiceGetOrderSplitRuleListDetail(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.GetOrderSplitRuleListDetailReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.GetOrderSplitRuleListDetail(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 重发支付短信验证码
func OrderServiceResendPaySMS(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.ResendPaySMSReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.ResendPaySMS(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

// 订单关闭
func OrderServiceCloseOrder(params map[string]interface{}) (map[string]interface{}, error) {
	req := allinpayService.CloseOrderReq{}
	gconv.Struct(params, &req)
	res, err := Pay.OrderService.CloseOrder(req)
	if err != nil {
		return nil, err
	}
	result := gconv.Map(res)
	return result, nil
}

func Call(name string, params ...interface{}) (result []reflect.Value, err error) {
	f := reflect.ValueOf(funcAlias[name])
	if len(params) != f.Type().NumIn() {
		err = errors.New("The number of params is not adapted.")
		return
	}
	in := make([]reflect.Value, len(params))
	for k, param := range params {
		in[k] = reflect.ValueOf(param)
	}
	result = f.Call(in)
	return
}
